package com.shoesmail.utils;

import com.shoesmail.constants.MinioPhotoConstants;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;

/**
 * @author ：CDB
 * @description：照片连接获取
 * @date ：2025/9/18 17:01
 */
@Component
@Slf4j
public class PhotoUtil {


    @Autowired
    private StringRedisTemplate stringRedisTemplate;
    @Autowired
    private MinioUtil minioUtil;
    // 默认图片，colour为0
    public String getPhotoUrl(int brand,int category,int colour){
        String key = getKey(brand, category, colour);
        String redisKey = key.replace(".",":");
        String photoUrl = stringRedisTemplate.opsForValue().get(redisKey);
        if(StringUtils.isNull(photoUrl)){
            synchronized (PhotoUtil.class){
                if(StringUtils.isNull(photoUrl)){
                    try {
                        photoUrl = minioUtil.getPhotoUrl(key);
                        stringRedisTemplate.opsForValue().set(redisKey,photoUrl);
                    } catch (Exception e) {
                        throw new RuntimeException(e);
                    }
                }
            }
        }
        return photoUrl;
    }

    private static String getKey(int brand,int category,int colour){
        String key = MinioPhotoConstants.PHOTO_PRE+MinioPhotoConstants.BRAND.replace("{}",String.valueOf(brand))
                +MinioPhotoConstants.CATEGORY.replace("{}",String.valueOf(category))+MinioPhotoConstants.COLOUR.replace("{}",String.valueOf(colour));
        log.info(key);
        return key;

    }



}
